package com.zufe.StudentSystem;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class UserSystem {
    public static void main(String[] args) {
        ArrayList<User> users = new ArrayList<User>();
        Scanner sc = new Scanner(System.in);
        boolean flag = true;
        while (flag) {
            System.out.println("欢迎来到学生管理系统");
            System.out.println("请选择操作1登录 2注册 3忘记密码");
            String choice = sc.next();
            switch (choice) {
                case "1" -> login(users);
                case "2" -> register(users);
                case "3" -> forgetPassword(users);
                default -> {
                    System.out.println("输入有误");
                    flag = false;
                }
            }
        }
    }

    private static void login(ArrayList<User> users) {
        Scanner sc = new Scanner(System.in);
        loop:
        while (true) {
            System.out.println("请输入用户名:");
            String username = sc.next();
            if (!checkUsername(users, username)) {
                System.out.println("用户名不存在");
                continue;
            }
            int index = getIndex(users, username);
            User user = users.get(index);
            int count = 0;
            while (true) {
                count++;
                if (count > 3) {
                    System.out.println("密码输入次数过多");
                    break loop;
                }
                System.out.println("请输入密码:");
                String password = sc.next();
                if (!user.getPassword().equals(password)) {
                    System.out.println("密码错误");
                    continue;
                } else {
                    break;
                }
            }
            while (true) {
                System.out.println("请输入验证码:");
                String code1 = createRandomCode();
                System.out.println("验证码:" + code1);
                String code = sc.next();
                if (code.equals(code1)) {
                    System.out.println("登录成功");
                    StudentSystem.start();
                    break loop;
                }
                System.out.println("验证码错误");
            }
        }
    }

    private static void register(ArrayList<User> users) {
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("请输入用户名:");
            String username = sc.next();
            if (checkUsername(users, username)) {
                System.out.println("用户名已存在");
            } else {
                String password = null;
                while (true) {
                    System.out.println("请输入密码:");
                    password = sc.next();
                    System.out.println("请再次输入密码:");
                    String password2 = sc.next();
                    if (!password.equals(password2)) {
                        System.out.println("两次密码不一致");
                        continue;
                    } else {
                        break;
                    }
                }
                String card = null;
                while (true) {
                    System.out.println("请输入身份证号:");
                    card = sc.next();
                    if (!checkCard(users, card)) {
                        System.out.println("身份证号格式错误");
                        continue;
                    } else {
                        break;
                    }
                }
                String phone = null;
                while (true) {
                    System.out.println("请输入手机号:");
                    phone = sc.next();
                    if (!checkPhone(users, phone)) {
                        System.out.println("手机号格式错误");
                        continue;
                    } else {
                        break;
                    }
                }
                users.add(new User(username, password, card, phone));
                System.out.println("注册成功");
                break;
            }
        }
    }

    private static void forgetPassword(ArrayList<User> users) {
        Scanner sc = new Scanner(System.in);
        loop:
        while (true) {
            System.out.println("请输入用户名:");
            String username = sc.next();
            if (!checkUsername(users, username)) {
                System.out.println("用户名不存在");
                continue;
            }
            int index = getIndex(users, username);
            User user = users.get(index);
            System.out.println("请输入身份证号:");
            String card = sc.next();
            if (!card.equals(user.getCard())) {
                System.out.println("身份证号错误");
                continue;
            }
            System.out.println("请输入手机号:");
            String phone = sc.next();
            if (!phone.equals(user.getPhone())) {
                System.out.println("手机号错误");
                continue;
            }
            while (true) {
                String password = null;
                String password2 = null;
                System.out.println("请输入新密码:");
                password = sc.next();
                System.out.println("请再次输入新密码:");
                password2 = sc.next();
                if (!password.equals(password2)) {
                    System.out.println("两次密码不一致");
                    continue;
                }
                user.setPassword(password);
                System.out.println("修改成功");
                break loop;
            }
        }
    }

    private static int getIndex(ArrayList<User> users, String username) {
        for (int i = 0; i < users.size(); i++) {
            if (users.get(i).getUsername().equals(username)) {
                return i;
            }
        }
        return -1;
    }

    public static boolean checkUsername(ArrayList<User> users, String username) {
        return getIndex(users, username) != -1;
    }

    public static boolean checkCard(ArrayList<User> users, String card) {
        if (card.length() != 18) {
            return false;
        }
        if (card.charAt(0) == '0') {
            return false;
        }
        for (int i = 0; i < 17; i++) {
            if (card.charAt(i) < '0' || card.charAt(i) > '9') {
                return false;
            }
        }
        return (card.charAt(17) >= '0' && card.charAt(17) <= '9') || card.charAt(17) == 'X';
    }

    public static boolean checkPhone(ArrayList<User> users, String phone) {
        if (phone.length() != 11) {
            return false;
        }
        for (int i = 0; i < 11; i++) {
            if (phone.charAt(i) < '0' || phone.charAt(i) > '9') {
                return false;
            }
        }
        return true;
    }

    public static String createRandomCode() {
        char[] letters = new char[52];
        char[] codes = new char[5];
        for (int i = 0; i < letters.length; i++) {
            if (i < 26) {
                letters[i] = (char) ('A' + i);
            } else {
                letters[i] = (char) ('a' + i - 26);
            }
        }
        Random r = new Random();
        for (int i = 0; i < 4; i++) {
            codes[i] = letters[r.nextInt(52)];
        }
        codes[4] = (char) (r.nextInt(10) + '0');

        int index = r.nextInt(5);
        char temp = codes[4];
        codes[4] = codes[index];
        codes[index] = temp;
        return new String(codes);
    }
}
